package com.neu.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.neu.R;
import com.neu.adapter.RecServiceAdapter;
import com.neu.bean.Rotation;
import com.neu.bean.ServiceList;
import com.neu.bean.Weather;
import com.neu.ui.BaseFragment;
import com.neu.util.VolleyUtil;

import java.util.List;

public class HomeFragment extends BaseFragment {

    private ViewFlipper vf;
    private HomeViewModel model;
    private RecyclerView recService;
    private TextView tvmaxTemperature;  //最高温度
    private TextView tvuv; //当前风力
    private TextView tvminTemperature;//最低温度
    private TextView tvweather;//当前天气


    @Override
    protected int layoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {
        vf=view.findViewById(R.id.vf);
        recService=view.findViewById(R.id.recService);
        // 5列一行
        recService.setLayoutManager(new GridLayoutManager(getContext(),5));

        tvmaxTemperature=view.findViewById(R.id.tvmaxTemperature);
        tvminTemperature=view.findViewById(R.id.tvminTemperature);
        tvuv=view.findViewById(R.id.tvuv);
        tvweather=view.findViewById(R.id.tvweather);
    }

    @Override
    protected void initData(final Context context) {
        //获得model对象的实例
        model=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory())
                .get(HomeViewModel.class);
        //实现广告轮播
        initRotation();
        //实现所有服务
        initService();
        //实现天气
        initWeather();
    }

    private void initWeather() {
        model.getWeather().observe(getViewLifecycleOwner(), new Observer<Weather>() {
            @Override
            public void onChanged(Weather weather) {
                tvmaxTemperature.setText(tvmaxTemperature.getText()+weather.getData().getMaxTemperature()+"°C");
                tvminTemperature.setText(tvminTemperature.getText()+weather.getData().getMinTemperature()+"°C");
                tvuv.setText(tvuv.getText()+weather.getData().getUv());
                tvweather.setText(tvweather.getText()+weather.getData().getWeather());
            }
        });
    }

    private void initService() {
        model.getServiceList().observe(getViewLifecycleOwner(), new Observer<ServiceList>() {
            @Override
            public void onChanged(ServiceList serviceList) {
                List<ServiceList.RowsBean> list=serviceList.getRows();
                //绑定数据
                recService.setAdapter(new RecServiceAdapter(list));

            }
        });
    }

    private void initRotation() {
        model.getRoation().observe(getViewLifecycleOwner(), new Observer<Rotation>() {
            @Override
            public void onChanged(Rotation rotation) {
                List<Rotation.RowsBean> list=rotation.getRows();
                for(Rotation.RowsBean r:list){
                    String url=r.getAdvImg();
                    ImageView iv=new ImageView(getContext());
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    Glide.with(getContext()).load(VolleyUtil.BASE_URL+url).into(iv);
                    vf.addView(iv);
                }
            }
        });
    }
}